<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图书列表展示</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">

    <link rel="stylesheet" href="css/list.css">
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script src="js/jq-paginator.js"></script>

    <style>
        .bookContainer {
            width: 60%;
            margin: 3em auto;
        }

        .bookContainer h2 {
            text-align: center;
            font-weight: bold;
            margin: 1em 0;
        }

        .btn-outline-info {
            margin-left: 10px;
            /* border-color:#dee2e6 ; */
        }

        .navbar-justify-between {
            display: flex;
            justify-content: space-between;
            margin-bottom: 10px;
        }

        .bookContainer .navbar .form-inline {
            margin-right: 20px;
        }

        .book-select {
            zoom: 130%;
        }

        .pagination {
            margin-top: 30px;
        }

        .page-link {
            color: #17a2b8;
            /* border-color: #17a2b8; */
        }

        .page-item.active .page-link {
            z-index: 3;
            color: #fff;
            background-color: #17a2b8;
            border-color: #17a2b8;
        }

        table {
            width: 100%;
            margin: 0 auto;
            border-collapse: collapse;
        }

        table caption {
            font-size: 2em;
            font-weight: bold;
            margin: 1em 0;
        }

        th,
        td {
            border: 1px solid #999;
            text-align: center;
            padding: 20px 0;
        }

        table thead tr {
            background-color: #0547c1;
            color: #fff;
        }

        table tbody tr:nth-child(odd) {
            background-color: #eee;
        }

        table .op {
            margin: 0 auto;
            width: 35%;
            display: flex;
            justify-content: space-between;
        }

        td .width100 {
            width: 100px;
        }

        .width200 {
            width: 200px;
        }
    </style>

</head>

<body>
    <div class="bookContainer">
        <h2>图书列表展示</h2>
        <div class="navbar-justify-between">
            <div>
                <button class="btn btn-outline-info" type="button" onclick="location.href='book_add.html'">添加图书</button>
                <button class="btn btn-outline-info" type="button" onclick="batchDelete()">批量删除</button>
            </div>
        </div>

        <table>
            <thead>
                <tr>
                    <td>选择</td>
                    <td class="width100">图书ID</td>
                    <td>书名</td>
                    <td>作者</td>
                    <td>数量</td>
                    <td>定价</td>
                    <td>出版社</td>
                    <td>状态</td>
                    <td class="width200">操作</td>
                </tr>
            </thead>
            <tbody>
                <!-- getBookList(); -->
            </tbody>
        </table>

        <script src="./jquery-3.7.1.min.js"></script>

        <script>
            getBookList();
            function getBookList() {
                $.ajax({
                    type: "get",
                    url: "/book/getlist",
                    success: function (result) {
                        if (result != null) {
                            //拼接
                            var finalHtml = "";
                            for (var book of result) {
                                finalHtml += '<tr>';
                                finalHtml += '<td><input type="checkbox" name="selectBook"value = "' + book.book_id + '" book.id = "selectBook" class="book-select" ></td > ';
                                finalHtml += '<td>' + book.book_id + '</td>';
                                finalHtml += '<td>' + book.book_name + '</td>';
                                finalHtml += '<td>' + book.book_author + '</td>';
                                finalHtml += '<td>' + book.book_number + '</td>';
                                finalHtml += '<td>' + book.book_price + '</td>';
                                finalHtml += '<td>' + book.book_publish + '</td>';
                                finalHtml += '<td>' + book.book_status_CN + '</td>';
                                finalHtml += '<td><div class="op">';
                                finalHtml += '<a href="book_update.html?bookId=' + book.id
                                    + '">修改</a>';
                                finalHtml += '<a href="javascript:void(0)"onclick = "deleteBook(' + book.book_id + ')" > 删除</a >';
                                finalHtml += '</div></td>';
                                finalHtml += "</tr>";
                            }
                            $("tbody").html(finalHtml);
                        }
                    }

                });
            }


            function deleteBook(id) {
                var isDelete = confirm("确认删除?");
                if (isDelete) {
                    //删除图书
                    alert("删除成功");
                }
            }
            function batchDelete() {
                var isDelete = confirm("确认批量删除?");
                if (isDelete) {
                    //获取复选框的id
                    var ids = [];
                    $("input:checkbox[name='selectBook']:checked").each(function () {
                        ids.push($(this).val());
                    });
                    console.log(ids);
                    alert("批量删除成功");
                }
            }

        </script>
    </div>
</body>

</html>